package doc2db.interfaces.elements;

import doc2db.Keey;
import doc2db.Launcher;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.LayoutStyle.ComponentPlacement;

/**
 *
 * @author yura
 */
public class AboutBox extends JDialog {
    private JButton closeButton = new JButton(new AbstractAction("close") {
            @Override public void actionPerformed(ActionEvent arg0) {
                closeAboutBox();
            }
        });
    private JLabel appTitleLabel = new JLabel();
    private JLabel versionLabel = new JLabel();
    private JLabel appVersionLabel = new JLabel();
    private JLabel vendorLabel = new JLabel();
    private JLabel appVendorLabel = new JLabel();
    private JLabel homepageLabel = new JLabel();
    private JLabel appHomepageLabel = new JLabel();
    private JLabel appDescLabel = new JLabel();
    private JLabel imageLabel = new JLabel();


    public AboutBox (Frame parent) {
        super(parent);
        this.sets();
        this.initComponents();
        this.addingComponents();
    }

    //@Action
    public void closeAboutBox() {
        dispose();
    }

    private void addingComponents() {
        JPanel tp = new JPanel();
        GroupLayout layout = new GroupLayout(tp);
        tp.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(versionLabel)
                            .addComponent(vendorLabel)
                            .addComponent(homepageLabel))
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(appVersionLabel)
                            .addComponent(appVendorLabel)
                            .addComponent(appHomepageLabel)))
                    .addComponent(imageLabel, GroupLayout.Alignment.CENTER) //TODO
                    .addComponent(appTitleLabel, GroupLayout.Alignment.CENTER)
                    .addComponent(appDescLabel, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                    .addComponent(closeButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imageLabel)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(appTitleLabel)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(appDescLabel)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(versionLabel)
                    .addComponent(appVersionLabel))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(vendorLabel)
                    .addComponent(appVendorLabel))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(homepageLabel)
                    .addComponent(appHomepageLabel))
                .addPreferredGap(ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addComponent(closeButton)
                .addContainerGap())
        );

        Container c = getContentPane();
        c.add(tp, BorderLayout.CENTER);
        pack();
    }

    private void initComponents() {
        final int b = Font.BOLD;
        final int ts = appTitleLabel.getFont().getStyle();
        final int tz = appTitleLabel.getFont().getSize() + 4;
        
        final int vrs = versionLabel.getFont().getStyle();
        final int vns = vendorLabel.getFont().getStyle();
        final int hs = homepageLabel.getFont().getStyle();
        
        setTitle(Launcher.getString("aboutbox.title"));


        //closeButton.setAction(actionMap.get("closeAboutBox"));
        closeButton.setName("closeButton");
        
        appTitleLabel.setFont(appTitleLabel.getFont().deriveFont(ts | b,tz));
        appTitleLabel.setText(Launcher.getString("Application.title"));
        appTitleLabel.setName("appTitleLabel");  

        versionLabel.setFont(versionLabel.getFont().deriveFont(vrs | b));
        versionLabel.setText(Launcher.getString("versionLabel.text"));
        versionLabel.setName("versionLabel");  

        appVersionLabel.setText(Launcher.getString("Application.version"));
        appVersionLabel.setName("appVersionLabel");  

        vendorLabel.setFont(vendorLabel.getFont().deriveFont(vns | b));
        vendorLabel.setText(Launcher.getString("vendorLabel.text"));
        vendorLabel.setName("vendorLabel");  

        appVendorLabel.setText(Launcher.getString("Application.vendor"));
        appVendorLabel.setName("appVendorLabel");  

        homepageLabel.setFont(homepageLabel.getFont().deriveFont(hs | b));
        homepageLabel.setText(Launcher.getString("homepageLabel.text"));
        homepageLabel.setName("homepageLabel");  

        final String s1 = Launcher.getString("Application.homepage");
        appHomepageLabel.setText("<html><i color=blue><u>" + s1);
        appHomepageLabel.setName("appHomepageLabel");  

        appDescLabel.setText(Launcher.getString("aboutbox.appDescLabel.text"));
        appDescLabel.setName("appDescLabel");  
        
        imageLabel.setIcon(Launcher.getIcon("aboutbox.imageLabel.icon"));
        imageLabel.setName("imageLabel");

        appHomepageLabel.addMouseListener(new MouseAdapter() {
            @Override public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1)
                    Keey.openBrowser(s1);
            }
        });
        appHomepageLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        appHomepageLabel.setToolTipText(homepageLabel.getText() + s1);

        
    }

    private void sets() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        setName("aboutBox");
        setResizable(false);
        //setSize(380, 400);
        getRootPane().setDefaultButton(closeButton);
    }
}
